import QtQuick 2.0
import QtQuick.Controls 1.1
import QtQuick.Controls.Styles 1.1
import "../FKWidget/"

FKWidget{
    id: updateWidget
    relativeHeight: 0.5
    relativeWidth: 0.8

    signal download
    signal restart
    signal interrupt
    signal skip
    signal cancel

    property var versionModel: appVersionModel

    FKViewport{
        TableView{
            id:tableView
            anchors{
                left:parent.left
                top:parent.top
                bottom:pb.top
                right:rightArea.left
                margins: 10
            }
            TableViewColumn{role: "needDL"; title: qsTr("DL"); width: 50}
            TableViewColumn{role: "fileName"; title: qsTr("File"); width: 150}
            TableViewColumn{role: "hostVersion"; title: qsTr("Actual version"); width: 100}
            TableViewColumn{role: "hostWeightLbl"; title: qsTr("Actual file size"); width: 100}
            TableViewColumn{role: "clientVersion"; title: qsTr("Current version"); width: 100}
            TableViewColumn{role: "clientWeightLbl"; title: qsTr("Current file size"); width: 100}
            TableViewColumn{role: "downloadedWeightLbl"; title: qsTr("Loaded"); width: 100}
            TableViewColumn{role: "needRE"; title: qsTr("Replace"); width: 50}
            model: appVersionFilteredModel
        }
        Item{
            id:rightArea
            anchors{
                right:parent.right
                top:parent.top
                bottom:parent.bottom
                margins: 10
            }
            width:parent.width*0.15
            CheckBox{
                id:option1
                anchors{
                    top:parent.top
                    right:parent.right
                    left:parent.left
                }
                text: qsTr("Show files for current OS only")
                checked: true
                style: CheckBoxStyle{
                    label: Text{
                        text:option1.text
                        wrapMode: Text.Wrap
                        width:option1.width*0.7
                    }
                }
                onCheckedChanged: appVersionFilteredModel.setFilterRegExp(checked ? versionModel.filteringExpr.toString() : "")
            }
            Column{
                anchors{
                    bottom:parent.bottom
                    left:parent.left
                    right:parent.right
                }
                Button{
                    text:qsTr("Download")
                    width:rightArea.width
                    onClicked: updateWidget.download()
                }
                Button{
                    text:qsTr("Replace and restart")
                    width:rightArea.width
                    onClicked: updateWidget.restart()
                }
                Button{
                    text:qsTr("Stop downloading")
                    width:rightArea.width
                    onClicked: updateWidget.interrupt()
                    enabled: false
                }
                Button{
                    text:qsTr("Skip")
                    width:rightArea.width
                    onClicked: submitter.ask(qsTr("Are you shure want skip updating? This can lead unexpected behaivour or crashes."))
                }
                Button{
                    text:qsTr("Cancel")
                    width:rightArea.width
                    onClicked: updateWidget.cancel()
                }
            }
        }
        Item{
            id:pb
            height: bar.height
            anchors{
                bottom:parent.bottom
                left:parent.left
                right:rightArea.left
                margins:10
            }
            Text{
                id:totalLoaded
                text:versionModel.totalDownloadedLbl
                width: 70
                anchors.left: parent.left
                anchors.bottom:parent.bottom
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                height: bar.height
            }
            ProgressBar{
                id:bar
                anchors.left: totalLoaded.right
                anchors.right: totalToLoad.left
                anchors.bottom:parent.bottom
                minimumValue: -1
                maximumValue: versionModel.totalDownload
                value: versionModel.totalDownloaded
            }
            Text{
                id:totalToLoad
                anchors.right: parent.right
                anchors.bottom:parent.bottom
                text: versionModel.totalDownloadLbl
                width: 70
                horizontalAlignment: Text.AlignHCenter
                verticalAlignment: Text.AlignVCenter
                height: bar.height
            }
        }
    }
    FKYesNoWidget{
        id:submitter
        hideAnimation: true
        onYes: updateWidget.skip()
    }
}
